﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PhotoList.Dal
{
    public class User
    {
        #region Attributes

        private string t_uid = "";
        public string Uid
        {
            set { this.t_uid = value.Trim(); }
            get { return this.t_uid.Trim(); }
        }

        private string t_pass="";
        public string Pass
        {
            set { this.t_pass = value.Trim(); }
            get { return this.t_pass.Trim(); }
        }

        private string t_home="";
        public string Home
        {
            set{this.t_home=value.Trim();}
            get{return this.t_home.Trim();}
        }

        private string t_name = "";
        public string Name
        {
            set { this.t_name = value.Trim(); }
            get { return this.t_name.Trim(); }
        }

        private string t_group = "User";
        public string Group
        {
            set { this.t_group = value.Trim(); }
            get { return this.t_group.Trim(); }
        }

        #endregion

        public User()
        {
        }

        public User(string uID)
        {
            this.t_uid = uID;
            LoadMe();
        }

        #region Method

        private void LoadMe()
        {
            DBO.UserStruct us = DBO.LoadUser(this.t_uid);
            if (us.uid != "")
            {
                this.t_uid = us.uid;
                this.t_pass = us.pass;
                this.t_home = us.home;
                this.t_name = us.name;
                this.t_group = us.group;
            }
        }

        public bool Login()
        {
            DBO.UserStruct us = DBO.LoadUser(this.t_uid);
            if(us.uid!=""&&us.pass==this.t_pass)
            {
                return true;
            }

            return false;
        }

        public bool IsRoot()
        {
            if (DBO.Read("/ROOT/UserList/Record[@userid='" + this.t_uid + "']", "group") == "Root")
            {
                return true;
            }

            return false;
        }

        public bool AddUser()
        {
            return DBO.AddUser(this.t_uid, this.t_pass, this.t_home, this.t_name, this.t_group);
        }

        public bool ModiUser()
        {
            return DBO.ModiUser(this.t_uid, this.t_pass, this.t_home, this.t_name, this.t_group);
        }

        public bool DeleUser()
        {
            return DBO.DeleUser(this.t_uid);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public bool ChangePwd()
        {
            if (DBO.Update("/ROOT/UserList/Record[@userid='"+this.t_uid+"']/pass", "", this.t_pass.Trim()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
